//
//  DigitAlbumDetailsViewController.m
//  NewApp
//
//  Created by  on 12-6-30.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "DigitAlbumDetailsViewController.h"
#import "ShowImageViewController.h"
#import "NAImage.h"
#import "UINavigationBar+Additions.h"
#import "DigitAlbumLargeImageViewController.h"
#import "ProductData.h"

@interface DigitAlbumDetailsViewController ()

@end

@implementation DigitAlbumDetailsViewController
@synthesize galleryView;


- (id) initWithAlbum:(Entity *) album {
    if (self = [super init]) {
        currentAlbum = [album retain];
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    currentPage = 1;
    
    ThumbnailGalleryView *v = [[ThumbnailGalleryView alloc] initWithFrame:CGRectMake(0.f, 35.f, 320.f, 367.f)];
    self.galleryView  = v;
    [v release];
    [self.galleryView setPerferRows:4];
    [self.galleryView setDelegate:self];
    [self.view addSubview:self.galleryView];
    
    //Send request
    [self.engine digitAlbumDetailsInfoWithID:currentAlbum.sID page:currentPage];
}

- (void) dealloc {
    [currentAlbum release];
    [galleryView release];
    [super dealloc];
}

- (void) requestDidFinished:(RequestEngine *)reqEngine withObject:(id)object {
    if ([object isKindOfClass:[NSDictionary class]]) {
        NSDictionary *result = (NSDictionary *) object;
        [result retain];
        
        self.titleLabel.text = [result objectForKey:kTopTitle];
        
        NSArray *thumbnails = [[result objectForKey:kData] retain];
        thumbnailImages = [[NSMutableArray alloc] init];
        
        for (NSDictionary *dic in thumbnails) {
            NAImage *image = [[NAImage alloc] initWithDictionary:dic];
            [thumbnailImages addObject:image];
        }
        
        [self.galleryView setGalleryWithLargeImages:thumbnailImages];
        
        dataArray = [[NSMutableArray alloc] initWithArray:thumbnailImages];
        
        //add click the thumnail event
        for (int i =0; i<[galleryView.imgsView count]; i++) {
            
            ((UIImageView *)[galleryView.imgsView objectAtIndex:i]).userInteractionEnabled=YES;
            UITapGestureRecognizer *singleTap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickImage:)];
            singleTap.numberOfTouchesRequired = 1; 
            singleTap.numberOfTapsRequired = 1; 
            [((UIImageView *)[galleryView.imgsView objectAtIndex:i]) addGestureRecognizer:singleTap];
            
        }
        
        //[thumbnailImages release];
        [thumbnails release];
        [result release];
    }
}


- (void) galleryImageDidPressed:(NAImage *)image {
    if (image.URL == nil) 
        return;
    
    
//    ShowImageViewController *showView = [[ShowImageViewController alloc] init];
//	showView.url = image.largeImageURL;    
//    //[self.navigationController pushViewController:showView animated:YES];
//     
////    DigitAlbumLargeImageViewController *showView = [[DigitAlbumLargeImageViewController alloc] init];
////    showView.smallImgArray = [[NSArray alloc] initWithArray:thumbnailImages];
////    showView.imageId = @"1";
////    showView.nextPage = 2;
//	UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:showView];
//    [navCtrl.navigationBar setBkImage:[UIImage imageNamed:@"top_logo.png"]];
//    navCtrl.navigationBar.barStyle = UIBarStyleDefault;
//	
//	[self presentModalViewController:navCtrl animated:YES];
//	
//	[showView release];
//	[navCtrl release];
    
//    FGalleryViewController *networkGallery = [[FGalleryViewController alloc] initWithPhotoSource:self];
 //   [self.navigationController pushViewController:networkGallery animated:YES];
 //   [networkGallery release];

}

- (BOOL) isInRect:(CGPoint)center fromPoint:(CGPoint)start toPoint:(CGPoint)end{
    
    if ((center.x < end.x && center.y < end.y)&&(center.x > start.x && center.y > start.y) ){
        return YES;
    }
    return NO;
}

- (void) onClickImage:(UITapGestureRecognizer *)sender{
    
    int n = 0;
    
    NSLog(@"Click point:x=%f,y=%f",sender.view.center.x,sender.view.center.y);
    
    while (n<[galleryView.imgsRect count]) {
        
        ImgRect *imgRect = (ImgRect *)[galleryView.imgsRect objectAtIndex:n];
        
        NSLog(@"Image range from x=%f,y=%f to x=%f,y=%f ",imgRect.start.x,imgRect.start.y,imgRect.end.x,imgRect.end.y);
        
        if ([self isInRect:sender.view.center fromPoint:imgRect.start toPoint:imgRect.end])
            break;
        else {
            n++;
        }
    }
    
	if (sender.numberOfTouchesRequired == 1 && n < [galleryView.imgsRect count]) {
        //to send request for the detail info.
        NSLog(@"You just click the iamge ID:%@",((ProductEntity *)[dataArray objectAtIndex:n]).ID);
        
        DigitAlbumLargeImageViewController *showView = [[DigitAlbumLargeImageViewController alloc] init];
        showView.smallImgArray = [[NSArray alloc] initWithArray:galleryView.imgsView];
        showView.imageId = ((ProductEntity *)[dataArray objectAtIndex:n]).ID;
        showView.nextPage = n+2;
        showView.index = n+1;
        self.title = @"返回";
        [self.navigationController pushViewController:showView animated:YES];
//        UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:showView];
//        [navCtrl.navigationBar setBkImage:[UIImage imageNamed:@"top_logo.png"]];
//        navCtrl.navigationBar.barStyle = UIBarStyleDefault;
//        
//        [self presentModalViewController:navCtrl animated:YES];
        
        [showView release];
//        [navCtrl release];

        
	}
}

#pragma mark - FGalleryViewControllerDelegate Methods


- (int)numberOfPhotosForPhotoGallery:(FGalleryViewController *)gallery {
	return thumbnailImages.count;
}


- (FGalleryPhotoSourceType)photoGallery:(FGalleryViewController *)gallery sourceTypeForPhotoAtIndex:(NSUInteger)index {
    return FGalleryPhotoSourceTypeNetwork;
}


- (NSString*)photoGallery:(FGalleryViewController *)gallery captionForPhotoAtIndex:(NSUInteger)index {
	return @"";
}


//- (NSString*)photoGallery:(FGalleryViewController*)gallery filePathForPhotoSize:(FGalleryPhotoSize)size atIndex:(NSUInteger)index {
//    return [localImages objectAtIndex:index];
//}

- (NSString*)photoGallery:(FGalleryViewController *)gallery urlForPhotoSize:(FGalleryPhotoSize)size atIndex:(NSUInteger)index {
    return [ ((NAImage *)[thumbnailImages objectAtIndex:index]).URL absoluteString];
}

- (void)handleTrashButtonTouch:(id)sender {
    // here we could remove images from our local array storage and tell the gallery to remove that image
    // ex:
    //[localGallery removeImageAtIndex:[localGallery currentIndex]];
}


- (void)handleEditCaptionButtonTouch:(id)sender {
    // here we could implement some code to change the caption for a stored image
}



@end
